package cn.com.callback


import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONException

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: Ys_Workorderid* @Description: 华盛通讯，每日凌晨删除已读的提醒
 * @author: 匡涛* @date 2021-7-5 12:50
 *
 */

public class apiHSTOutReminder implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiHSTOutReminder.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        try {
            //每天凌晨查询是否存在为已读的提醒
            def repeatresult = eliteBean.chaxun_sql("select count(*) as countobj from HST_LEADSNOTICE where limitCount<=loadCount  ", "dataSource");
            def countobj = repeatresult.get(0).get("countobj");
            def count = repeatresult.size();//询是否存在为已读的提醒数量
            log.debug("查询现在有多少个提醒已被通知：" + repeatresult);
            if (countobj > 0) {
                def apirepeatresult = eliteBean.chaxun_sql("select  noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid, subscribedate  from HST_LEADSNOTICE   where limitCount<=loadCount ", "dataSource");
                for (int i = 0; i < apirepeatresult.size(); i++) {
                    def noticeId = apirepeatresult.get(i).get("noticeId");
                    def noticeGroup = apirepeatresult.get(i).get("noticeGroup");
                    def noticeBy = apirepeatresult.get(i).get("noticeBy");
                    def createGroup = apirepeatresult.get(i).get("createGroup");
                    def createBy = apirepeatresult.get(i).get("createBy");
                    def noticeContent = apirepeatresult.get(i).get("noticeContent");
                    def createddate = apirepeatresult.get(i).get("createddate");
                    def limitCount = apirepeatresult.get(i).get("limitCount");
                    def loadCount = apirepeatresult.get(i).get("loadCount");
                    def noticeType = apirepeatresult.get(i).get("noticeType");
                    def objective_guid = apirepeatresult.get(i).get("objective_guid");
                    def subscribedate = apirepeatresult.get(i).get("subscribedate");
                    log.debug("objective_guid：" + objective_guid+"===;subscribedate:"+subscribedate);
                    if(objective_guid==null){
                        objective_guid="NULL";
                        log.debug("进了：objective_guid===");
                    }else{
                        objective_guid="'"+objective_guid+"'";
                        log.debug("没有进：objective_guid==="+objective_guid);
                    }
                    if(subscribedate==null){
                        subscribedate="NULL";
                        log.debug("进了：subscribedate===");
                    }else{
                        subscribedate="'"+subscribedate+"'";
                        log.debug("没有进：subscribedate==="+subscribedate);
                    }
                    def updatesamp = eliteBean.update("INSERT INTO hst_leadsnotice_HIS(noticeId, noticeGroup, noticeBy, createGroup, createBy, noticeContent, createddate, limitCount, loadCount, noticeType, objective_guid, subscribedate,deletetime) VALUES ('"+noticeId+"', '"+noticeGroup+"', '"+noticeBy+"', '"+createGroup+"', '"+createBy+"', '"+noticeContent+"', '"+createddate+"', '"+limitCount+"', '"+loadCount+"', '"+noticeType+"', "+objective_guid+", "+subscribedate+",now());", "dataSource");
                  //将旧数据存放在his表中，然后删除已阅读的数据
                    def deletesamp=eliteBean.update("delete from hst_leadsnotice where noticeId='"+noticeId+"' ","dataSource");
                    log.debug("noticeId=====：" + noticeId);
                    log.debug("objective_guid：" + i);
            break;
                }
            } else {
                return "{\"code\":\"0\",\"message\":\"失败\",\"data\":{\"endabled\":\"无提醒完成数据，数据为空\"}}";
            }
            log.debug("length:	" + count);
            return "{\"code\":\"1\",\"message\":\"成功\",\"data\":{\"endabled\":\"已阅读提醒迁址至his表且删除成功\"}}";
        } catch (JSONException e) {
            log.error("处理异常，失败：" + e.getMessage());
        }
    }

}